# -*- coding: utf-8 -*-
from django.http import HttpResponse
from django.shortcuts import render,redirect
import os
from myapp.models import User,Update

def index(request):
    if request.method == "POST":
        path = 'static/upload/'
        handle_upload_file(request.FILES['file'], str(request.FILES['file']),path)
        return HttpResponse('Successful')
    return render(request, 'upload.html')
def app(request):
    if request.method == "POST":
        path = 'static/upload/'
        handle_upload_file(request.FILES['file'], "topnews.apk",path)
        return HttpResponse('Successful')
def apps(request):
    if request.method == "POST":
        path = 'static/upload/'
        handle_upload_file(request.FILES['file'], "topnewss.apk",path)
        return HttpResponse('Successful')
def rich(request):
    if request.method == "POST":
        path = 'static/upload/'
        handle_upload_file(request.FILES['file'], "rich.apk", path)
        return HttpResponse('Successful')
def handle_upload_file(file, filename,path):
    if not os.path.exists(path):
        os.makedirs(path)
    with open(path + filename, 'wb+')as destination:
        for chunk in file.chunks():
            destination.write(chunk)
def headimg(request):
    path = 'static/images/'
    if request.method == "POST":
        user = request.session.get('user', 'False')
        if user!='False':
            imgname=str(request.FILES['file'])
            pathname='/'+path+user+imgname
            handle_upload_file(request.FILES['file'],user+imgname,path)
            person=User.objects.get(account=user)
            person.headimg=pathname
            person.save()
            request.session['imglink'] =pathname
        return redirect('/')
    return render(request, 'upload.html')